//
//  AWSessionMetaData.m
//  AtWork2
//
//  Created by lucas on 15/7/16.
//  Copyright (c) 2015年 Foreveross. All rights reserved.
//

#import "AWSessionMetaData.h"
#import "ATWUtil.h"

@interface AWSessionMetaData ()

@property (nonatomic, copy, readwrite) NSString *host;

@property (nonatomic, assign, readwrite) UInt16 port;

@property (nonatomic, copy, readwrite) NSString *secret;

@end

@implementation AWSessionMetaData

+ (instancetype)sessionMetaDataWithHost:(NSString*)host port:(NSInteger)port secret:(NSString*)secret tenantId:(NSString*)tenanId username:(NSString*)username
{
    return [[self alloc] initWithHost:host port:port secret:secret tenantId:tenanId username:username];
}

+(instancetype)sessionMetaDataWithProtocolAddress:(NSString*)protocolAddress secret:(NSString*)secret tenanId:(NSString*)tenanId username:(NSString*)username
{
    return [[self alloc] initWithProtocolAddress:protocolAddress secret:secret tenanId:tenanId username:username];
}

- (instancetype)initWithHost:(NSString*)host port:(NSInteger)port secret:(NSString*)secret tenantId:(NSString*)tenanId username:(NSString*)username
{
    if (self = [super init])
    {
        self.host = host;
        self.port = port;
        self.secret = secret;
        self.tenantId = tenanId;
        self.username = username;
    }
    return self;
}

- (instancetype)initWithProtocolAddress:(NSString*)protocolAddress secret:(NSString*)secret tenanId:(NSString*)tenanId username:(NSString*)username
{
    if(self = [super init])
    {
        self.secret = secret;
        self.tenantId = tenanId;
        self.username = username;
        
        NSArray *host_port = [[[protocolAddress componentsSeparatedByString:@"://"] lastObject] componentsSeparatedByString:@":"];
        self.host = host_port.firstObject;
        self.port = [host_port.lastObject integerValue];
    }
    return self;
}

#pragma mark - getter

- (NSString *)deviceId
{
    if(!_deviceId)
    {
        _deviceId = [ATWUtil deviceId];
    }
    return _deviceId;
}

@end